﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

namespace ZyGame.Resource
{
    public class PackageInfo
    {
        public uint crc;
        public uint version;
        public long length;
        public string name;
        public string module;
        public List<string> files;
        public List<string> dependencies;

        public bool IsHaveAsset(string name)
        {
            if (name.StartsWith("Assets"))
            {
                return files.Contains(name);
            }
            return files.Where(x => Path.GetFileName(x) == name).Count() > 0;
        }

        [NonSerialized]
        private static List<PackageInfo> packages = new List<PackageInfo>();

        private static bool EnsureAssetBuilderConfigStatus()
        {
            if (packages.Count is 0)
            {
                return false;
            }
            return true;
        }

        public static void LoadModulePackageConfig(string module)
        {
            string fileListName = AppConfig.GetModuleFileListName(module);
            if (!Client.File.Exists(fileListName))
            {
                return;
            }
            string fileData = Client.File.ReadData(fileListName).GetString();
            if (!fileData.TryGetJson(out List<PackageInfo> list))
            {
                return;
            }
            packages.AddRange(list);
        }

        public static PackageInfo GetPackage(string name)
        {
            if (EnsureAssetBuilderConfigStatus() is false)
            {
                return default;
            }
            return packages.Find(x => name == x.name);
        }

        public static PackageInfo GetPackageWithAssetName(string url)
        {
            if (EnsureAssetBuilderConfigStatus() is false)
            {
                return default;
            }
            return packages.Find(x => x.IsHaveAsset(url));
        }

        public static List<PackageInfo> GetModulePackages(string module)
        {
            if (EnsureAssetBuilderConfigStatus() is false)
            {
                return default;
            }
            return packages.Where(x => x.module.ToLower() == module.ToLower()).ToList();
        }

        public static PackageInfo[] GetDependciesPackages(PackageInfo package)
        {
            if (package is null)
            {
                return Array.Empty<PackageInfo>();
            }
            List<PackageInfo> list = new List<PackageInfo>() { package };
            if (package.dependencies is not null && package.dependencies.Count is not 0)
            {
                for (int i = 0; i < package.dependencies.Count; i++)
                {
                    PackageInfo[] result = GetDependciesPackages(GetPackage(package.dependencies[i]));
                    for (int j = 0; j < result.Length; j++)
                    {
                        if (!list.Contains(result[j]))
                        {
                            continue;
                        }
                        list.Add(result[j]);
                    }
                }
            }
            return list.ToArray();
        }
    }
}
